﻿/*-----------------------------------------------
// File: ResultState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class ResultState : Fsm.FsmStateMonoBase<BattleState>
    {
        BattleViewCtrl viewCtrl
        {
            get { return this.controller as BattleViewCtrl; }
        }
        public ResultState(BattleState state, BattleViewCtrl ctrl) : base(state, ctrl) { }
        public override void Enter()
        {
            Debug.Log("Win Camp:" + viewCtrl.winCamp);
            if (StaticData.curBattleData.battleType == BattleType.PVE ||
                StaticData.curBattleData.battleType == BattleType.FIELD_PVE || 
                StaticData.curBattleData.battleType == BattleType.FIELD_BOSS)
            {
                StaticData.pveSpeedCache = BattleManager.Instance.curSpeedEnum;
            }
            else if (StaticData.curBattleData.battleType == BattleType.PVP
                || StaticData.curBattleData.battleType == BattleType.FIELD_PVP)
            {
                StaticData.pvpSpeedCache = BattleManager.Instance.curSpeedEnum;
            }
            else if (StaticData.curBattleData.battleType == BattleType.PLUNDER)
            {
                StaticData.pluderSpeedCache = BattleManager.Instance.curSpeedEnum;
            }
            else
            {

            }
            BattleManager.Instance.ResetSpeed();

            if (viewCtrl.winCamp == BattleCamp.Attacker)
            {
                CameraController.Instance.Win();
                BattleManager.Instance.battlePanel.ShowResult();
                if (!viewCtrl.isPlayedWinAudio)
                {
                    System.Collections.Generic.List<BattleCharacter> aliveBCs = new System.Collections.Generic.List<BattleCharacter>();
                    foreach (var uid in viewCtrl.GetActorUids(BattleCamp.Attacker))
                    {
                        var bc = viewCtrl.GetBattleCharacter(uid);
                        if (bc.isAlive)
                        {
                            aliveBCs.Add(bc);
                        }
                    }
                    int index = Random.Range(0, aliveBCs.Count);
                    aliveBCs[index].PlayWinAudio();
                    viewCtrl.isPlayedWinAudio = true;
                }
            }
            else
            {
                CameraController.Instance.Lose();
                BattleManager.Instance.battlePanel.ShowResult();
            }
            foreach (var uid in viewCtrl.GetActorUids(BattleCamp.Attacker))
            {
                var bc = viewCtrl.GetBattleCharacter(uid);
                bc.SwitchState(ActorState.Celebrate);
            }
            foreach (var uid in viewCtrl.GetActorUids(BattleCamp.Defender))
            {
                var bc = viewCtrl.GetBattleCharacter(uid);
                bc.SwitchState(ActorState.Celebrate);
            }
        }
    }
}
